package xyz.syyrjx.entity;

import java.io.Serializable;

/**
 * 结果封装类
 * @author Mr.Wan
 * @date 2022/10/10 - 21:29
 */
public class ResultInfo<T> implements Serializable {
    private final static long serialVersionUID = -1;
    //成功和失败的常量
    public final static boolean RESULT_SUCCESS = true;
    public final static boolean RESULT_FAIL = false;

    //成功/失败标志
    private boolean flag;
    //成功返回的数据
    private T data;
    //失败返回的提示信息
    private String message;

    private ResultInfo(){}

    //对成功数据进行封装
    public static <E> ResultInfo<E> success(E data){
        ResultInfo<E> resultInfo = new ResultInfo<>();
        resultInfo.flag = ResultInfo.RESULT_SUCCESS;
        resultInfo.data = data;
        return resultInfo;
    }

    //对失败信息进行封装
    public static <E> ResultInfo<E> fail(String message){
        ResultInfo<E> resultInfo = new ResultInfo<>();
        resultInfo.flag = ResultInfo.RESULT_FAIL;
        resultInfo.message = message;
        return resultInfo;
    }

    /** 只对外放开get权限 **/
    public boolean isFlag() {
        return flag;
    }

    public T getData() {
        return data;
    }

    public String getMessage() {
        return message;
    }
}
